package com.dv.shell.controller;

import com.dv.shell.entity.ProductCategory;
import com.dv.shell.entity.ProductInfo;
import com.dv.shell.service.CategoryServie;
import com.dv.shell.service.ProductService;
import com.dv.shell.vo.ProductInfoVo;
import com.dv.shell.vo.ProductVo;
import com.dv.shell.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author DavidLoman
 * @create 2021/3/28 13:16
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryServie categoryServie;

    @RequestMapping("/list")
    public ResultVo list() {

        List<ProductInfo> productInfos = productService.findUpAll();
        List<Integer> collect = productService.findUpAll().stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> categories = categoryServie.findByCategoryTypeIn(collect);

        ArrayList<ProductVo> productVoList = new ArrayList<>();
        for (ProductCategory productCategory : categories) {
            ProductVo productVo = new ProductVo();
            productVo.setCategoryName(productCategory.getCategoryName());
            productVo.setCategoryType(productCategory.getCategoryType());

            ArrayList<ProductInfoVo> productInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo : productInfos) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo, productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }
            }
            productVo.setProductInfoVoList(productInfoVoList);
            productVoList.add(productVo);
        }
        return ResultVo.success(productVoList);
    }

}
